package org.apache.ocean.main.search;

import java.util.Map;

import org.apache.lucene.util.PriorityQueue;
import org.apache.ocean.main.search.Search.Crowd;
import org.apache.ocean.main.search.Search.CrowdKey;
import org.apache.ocean.main.search.Search.CrowdValueResult;
import org.apache.ocean.main.search.Search.Crowds;
import org.apache.ocean.main.search.Search.ScoreDoc;
import org.apache.ocean.main.search.cache.FieldCacheKey.FieldIndex;
import org.apache.ocean.main.util.Constants;

/**
 * Allows inserts only if crowds have not been exceeded
 */
public abstract class CrowdPriorityQueue extends PriorityQueue {
	private Crowds crowds;
	private CrowdValueResult crowdValueResult;
	
	public CrowdPriorityQueue(int size, Crowds crowds) {
		initialize(size);
		this.crowds = crowds;
	}
	
	public static class CrowdResultBuilder {
		private Map<CrowdKey,CrowdValueResult> map; 
		
		public boolean add(CrowdValue[] crowdValues) {
		  CrowdValueResult crowdValueResult = map.get(crowdValue.getCrowdKey());
		  add(crowdValue, crowdValueResult);
		}
		
		private boolean add(CrowdValue crowdValue, CrowdValueResult crowdValueResult) {
			
		}
	}
	
	public static class CrowdValue {
		private String field;
		private Long id;
		private Comparable value;
		
		public CrowdValue(String field, Long id, Comparable value) {
			this.field = field;
			this.id = id;
			this.value = value;
		}
		
		public CrowdKey getCrowdKey() {
			
		}
	}
	
	public boolean insert(Object element) {
		ScoreDoc scoreDoc = (ScoreDoc)element;
		Comparable[] crowdValues = scoreDoc.crowdValues;
		for (int x=0; x < crowds.size(); x++) {
			Crowd crowd = crowds.get(x);
			Comparable value = crowdValues[x];
			FieldIndex fieldIndex = scoreDoc.indexSnapshot.getFieldIndex(Constants.ID);
			Long id = (Long)fieldIndex.getValue(scoreDoc.doc);
		  if (crowdValueResult.add(crowd, id, value)) {
		  	return super.insert(element);
		  } else {
		  	return false;
		  }
		}
	}
}
